Skip to content

[java] Add code examples to show how to use user context for single browser instance #2228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2025

Conversation

pujagani
Copy link
Contributor

@pujagani pujagani commented Mar 20, 2025

User description

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Motivation and Context

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Tests


Description

  • Added multiple test classes for parallel browser instance testing.

    • MultipleInstanceParallelTest tests background color changes using multiple browser instances.
    • SingleInstanceCookieParallelTest tests background color changes using a single browser instance with user context.
    • SingleInstanceCookieParallelGridTest tests similar functionality on a Selenium Grid setup.
  • Demonstrated the use of WebDriver BiDi API for user context and browsing context management.

  • Included assertions for verifying background color changes and default states.


Changes walkthrough 📝

Relevant files
Tests
MultipleInstanceParallelTest.java
Add tests for multiple browser instance parallel testing 

examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/MultipleInstanceParallelTest.java

  • Added tests for background color changes using multiple browser
    instances.
  • Utilized FirefoxDriver with private browsing mode.
  • Verified color changes to blue, green, and default white.
  • Included setup and cleanup methods for WebDriver lifecycle management.
  • +92/-0   
    SingleInstanceCookieParallelTest.java
    Add tests for single browser instance with user context   

    examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java

  • Added tests for background color changes using a single browser
    instance.
  • Demonstrated user context creation and browsing context management.
  • Verified color changes to blue, green, and default white.
  • Used WebDriver BiDi API for advanced interactions.
  • +124/-0 
    SingleInstanceCookieParallelGridTest.java
    Add Selenium Grid tests for single browser instance           

    examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java

  • Added tests for background color changes on Selenium Grid.
  • Demonstrated user context and browsing context creation on a remote
    WebDriver.
  • Verified color changes to blue, green, and default white.
  • Included @Ignore annotations for tests, likely for future enablement.
  • +139/-0 

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link

    netlify bot commented Mar 20, 2025

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit f946df9

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Ignored Tests

    All test methods in this class are annotated with @ignore but the class itself is not. Consider adding a comment explaining why these tests are ignored or remove the annotation if they should be executed.

    @Ignore
    @Test
    void canSwitchToBlue() {
      context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);
    
      RemoteValue value = context.locateNode(Locator.xpath("/html/body/button[1]"));
    
      Input inputModule = new Input(driver);
      Actions actions = new Actions(driver);
    
      RemoteWebElement element = new RemoteWebElement();
      element.setId(value.getSharedId().get());
      actions.moveToElement(element).click();
    
      inputModule.perform(context.getId(), actions.getSequences());
    
      value = context.locateNode(Locator.xpath("/html/body"));
    
      NodeProperties properties = (NodeProperties) value.getValue().get();
      String bgColor = properties.getAttributes().get().get("style");
    
      Assertions.assertEquals(bgColor, "background-color: lightblue;");
      System.out.println(
          Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
              .getMethodName() + " => executed successfully");
    }
    
    @Ignore
    @Test
    void canSwitchToGreen() {
      context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);
    
      RemoteValue value = context.locateNode(Locator.xpath("/html/body"));
    
      NodeProperties properties = (NodeProperties) value.getValue().get();
      String bgColor = properties.getAttributes().get().get("style");
    
      Assertions.assertEquals(bgColor, "background-color: white;");
    
      value = context.locateNode(Locator.xpath("/html/body/button[2]"));
    
      Input inputModule = new Input(driver);
      Actions actions = new Actions(driver);
    
      RemoteWebElement element = new RemoteWebElement();
      element.setId(value.getSharedId().get());
      actions.moveToElement(element).click();
    
      inputModule.perform(context.getId(), actions.getSequences());
    
      value = context.locateNode(Locator.xpath("/html/body"));
    
      properties = (NodeProperties) value.getValue().get();
      bgColor = properties.getAttributes().get().get("style");
    
      Assertions.assertEquals(bgColor, "background-color: lightgreen;");
      System.out.println(
          Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
              .getMethodName() + " => executed successfully");
    }
    
    @Ignore
    @Test
    void canHaveTheDefaultBackgroundColor() {
      context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);
    
      RemoteValue value = context.locateNode(Locator.xpath("/html/body"));
    
      NodeProperties properties = (NodeProperties) value.getValue().get();
      String bgColor = properties.getAttributes().get().get("style");
    
      Assertions.assertEquals(bgColor, "background-color: white;");
      System.out.println(
          Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
              .getMethodName() + " => executed successfully");
    }
    Hardcoded URL

    The test uses a hardcoded URL for the Selenium Grid (http://localhost:4444). Consider making this configurable through a property or environment variable to improve flexibility.

    new RemoteWebDriver(
        new URL("http://localhost:4444"),
        options, false);

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 20, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Use non-deprecated BiDi enablement

    Replace the deprecated setCapability("webSocketUrl", true) with
    options.enableBiDi() which is the recommended way to enable BiDi protocol
    support in Selenium 4.

    examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/MultipleInstanceParallelTest.java [19-21]

     FirefoxOptions options = new FirefoxOptions();
    -options.setCapability("webSocketUrl", true);
    +options.enableBiDi();
     options.addArguments("-private");
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies the use of a deprecated method setCapability("webSocketUrl", true) and proposes replacing it with the recommended options.enableBiDi() method. This is an important improvement for code maintainability and future compatibility with Selenium 4.

    Medium
    • Update

    @pujagani pujagani merged commit 0e7df55 into SeleniumHQ:trunk Mar 20, 2025
    7 of 9 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant